package com.game;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class PuzzleActivity extends Activity {
	
	private PuzzleView mPuzzleView;
	private MenuView mMenuView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        int bigstage = this.getIntent().getIntExtra("bigstage", 0);
        int smallstage = this.getIntent().getIntExtra("smallstage", 0);
        
        mPuzzleView = new PuzzleView(this);
        mPuzzleView.setStage(bigstage, smallstage);
        mPuzzleView.setOnButtonClickListener(new PuzzleView.OnButtonClickListener() {
			@Override
			public void onPause() {
				pause();
			}

			@Override
			public void onMainMenu() {
				mPuzzleView.recycleAll();
				finish();
				
			}
		});
        
        mMenuView = new MenuView(this);
        mMenuView.setOnButtonClickListener(new MenuView.OnButtonClickListener() {
			@Override
			public void onReplay() {
				
			}
			
			@Override
			public void onMainMenu() {
				mPuzzleView.recycleAll();
				finish();
			}
			
			@Override
			public void onClose() {
				resume();
			}

			@Override
			public void onMusicSwitch() {
				
			}

			@Override
			public void onSoundSwitch() {
				
			}
		});
        
		setContentView(mPuzzleView);
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        addContentView(mMenuView, lp);
        mMenuView.setVisibility(View.GONE);
    }

	@Override
	public void onBackPressed() {
		if (mMenuView.getVisibility() == View.VISIBLE) {
			resume();
		} else {
			pause();
		}
	}
	
	public void pause() {
		mMenuView.post(new Runnable() {
			@Override
			public void run() {
				mMenuView.show();
			}
		});
		
		mPuzzleView.puzzlePause();
	}
	
	public void resume() {
		mMenuView.post(new Runnable() {
			@Override
			public void run() {
				mMenuView.hide();
			}
		});
		
		mPuzzleView.puzzleResume();
	}
    
}